package com.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ICommonDataService;
import com.server.service.IForeignService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("foreign")
public class ForeignConroller {
	
	private String tableName = null;
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired 
	private IForeignService foreignService;
	@RequestMapping("country/{type}")
	public ModelAndView country(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		ModelAndView mv = new ModelAndView("foreign/countryList");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("type", type);
		//mv.addObject("state", state);
		return mv;
	}
	@RequestMapping("countryList/{type}/{state}")
	public void countyList(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type,
			@PathVariable Integer state){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String level = SessionUtil.getStringAttri(request, SessionData.levelKey_mng);
		String whereStr = "valid=1";
		if(StringUtils.isEmpty(level) || !level.equals("1")){
			whereStr+=" and creater='"+userName+"'";
		}
		if(state==1){
			whereStr+=" and org_or_pers='机构'";
		}
		if(state==2){
			whereStr+=" and org_or_pers='个人'";
		}
		String sortStr = "createTime desc";
		switch (type) {
		case 1://涉外国别
			tableName = "education.sicfa_con_reg_sit";
			break;
		case 2://涉外机构和人员
			tableName = "education.sicfa_for_ins_per";
			break;
		case 3://应急处理
			tableName = "education.sicfa_emr_hand";
			break;
		case 4://相关专题
			tableName = "education.sicfa_rel_sub";
			break;
		default:
			break;
		}
		DataVector<Map<String, Object>> listData = commonDataService.getListData(tableName, whereStr, sortStr, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@Login
	@RequestMapping("add/{type}")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/foreign/countryForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("type",type);
		mv.addObject("state", 0);
		mv.addObject("business", "foreign");
		switch (type) {
		case 1:
			mv.addObject("titleName", "新增涉外国别/地区形势资料");
			break;
		case 2:
			mv.addObject("titleName", "新增涉外机构信息");
			break;
		case 3:
			mv.addObject("titleName", "新增应急处理流程资料");
			break;
		case 4:
			mv.addObject("titleName", "新增相关专题资料");
			break;
		default:
			break;
		}
		
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("save/{id}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String formData = request.getParameter("formData");
		Map<String, Object> saveMap = JSONSerielizer.toMap(formData);
		String type = saveMap.get("type").toString();
		saveMap.remove("type");
		/*if(type.equals("3") || type.equals("4")){
			saveMap.put("class", saveMap.get("class1"));
			saveMap.remove("class1");
		}*/
		Boolean isSuccess = true;
		UUID uuid = UUID.randomUUID();
		Map<String, Object> map = new HashMap<String, Object>();
		switch (type) {
		case "1"://涉外国别
			tableName = "education.sicfa_con_reg_sit";
			break;
		case "2"://涉外机构和人员
			tableName = "education.sicfa_for_ins_per";
			break;
		case "3"://应急处理
			tableName = "education.sicfa_emr_hand";
			break;
		case "4"://相关专题
			tableName = "education.sicfa_rel_sub";
			break;
		default:
			break;
		}
		if (id.equals(0)) {
			if (type.equals("4")) {
				saveMap.put("uuid", uuid);
			}
			isSuccess = commonDataService.add(operator, tableName, saveMap);
			if(type.equals("4")){
				if(isSuccess){
					id = foreignService.getId(uuid);
					map.put("id", id);
				}
			}
		}else {
			isSuccess = commonDataService.update(operator, tableName, "id", id.toString(), saveMap);
		}
		map.put("isSuccess", isSuccess);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	/**
	 * 
	 * 通过id获取数据
	 * @param request
	 * @param response
	 * @param id
	 * @author xiaoxiaoping
	 * @date 2016年8月18日
	 */
	@RequestMapping("countryForm/{id}/{type}")
	public void form(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id,@PathVariable Integer type){
		switch (type) {
		case 1://涉外国别
			tableName = "education.sicfa_con_reg_sit";
			break;
		case 2://涉外机构和人员
			tableName = "education.sicfa_for_ins_per";
			break;
		case 3://应急处理
			tableName = "education.sicfa_emr_hand";
			break;
		case 4://相关专题
			tableName = "education.sicfa_rel_sub";
			break;
		default:
			break;
		}
		Map<String, Object> formData = commonDataService.getFormData(tableName, "id", id, null, null);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@Login
	@RequestMapping("update/{id}/{state}/{type}")
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/foreign/countryForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "edit");
		mv.addObject("type", type);
		mv.addObject("state", state);
		mv.addObject("business", "foreign");
		switch (type) {
		case 1:
			mv.addObject("titleName", "编辑国别/地区形势资料");
			break;
		case 2:
			mv.addObject("titleName", "编辑涉外机构信息");
			break;
		case 3:
			mv.addObject("titleName", "编辑应急处理流程资料");
			break;
		case 4:
			mv.addObject("titleName", "编辑相关专题资料");
			break;
		default:
			break;
		}
		
		mv.addObject("userName", userName);
		return mv;
	}
	/**
	 * 查看
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @param state
	 * @return
	 * @author xiaoxiaoping
	 * @date 2016年8月18日
	 */
	@Login
	@RequestMapping("check/{id}/{state}/{type}")
	public ModelAndView check(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state,@PathVariable Integer type) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/foreign/countryForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("state", state);
		mv.addObject("type", type);
		mv.addObject("business", "foreign");
		switch (type) {
		case 1:
			mv.addObject("titleName", "查看国别/地区形势资料");
			break;
		case 2:
			mv.addObject("titleName", "查看涉外机构信息");
			break;
		case 3:
			mv.addObject("titleName", "查看应急处理流程资料");
			break;
		case 4:
			mv.addObject("titleName", "查看相关专题资料");
			break;
		default:
			break;
		}
		
		mv.addObject("userName", userName);
		return mv;
	}
	/**
	 * 删除数据
	 * @param request
	 * @param response
	 * @author xiaoxiaoping
	 * @date 2016年8月18日
	 */
	@RequestMapping("delete/{type}")
	public void delete(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String ids = request.getParameter("ids");
		Integer estimate = foreignService.deleteC(ids, userName,type.toString());
		OutputUtil.returnString(response, estimate.toString());
	}
	/**
	 * 提交评估
	 * 
	 * @param request
	 * @param response
	 * @author xiaoxiaoping
	 * @date 2016年8月18日
	 */
	@Login
	@RequestMapping("toEstimate/{type}")
	public void toEstimate(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String ids = request.getParameter("ids");
		Integer estimate = foreignService.toEstimateC(ids, userName,type.toString());
		OutputUtil.returnString(response, estimate.toString());
	}
	@Login
	@RequestMapping("getEvalue/{id}/{typeId}")
	public void getEvalue(HttpServletRequest request,HttpServletResponse response,
			@PathVariable Integer id,@PathVariable Integer typeId){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		List<Map<String, Object>> list = foreignService.getEvalue(id, typeId);
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	@Login
	@RequestMapping("getAudit/{id}/{typeId}")
	public void getAudit(HttpServletRequest request,HttpServletResponse response,
			@PathVariable Integer id,@PathVariable Integer typeId){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		List<Map<String, Object>> list = foreignService.getAudit(id, typeId);
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
}
